<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class LoverConsumerDiamond extends Base
{
    protected $table = 'lover_diamond_consumer_log';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function create($user_id, $value)
    {
        $data['user_id'] = $user_id;
        $data['diamond_value'] = $value;
        return DB::table($this->table)->insert($data);
    }

    public function getLog($user_id)
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->first();
        if (!$res) {
            return false;
        }
        return $res;
    }

    public function getAllLog($user_id, $field = ['user_id'])
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->select($field)->get()->toArray();
        if (!$res) {
            return false;
        }
        return $res;
    }

    public function checkUserBag($user_id, $rank)
    {
        $where['user_id'] = $user_id;
        $where['rank'] = $rank;
        return DB::table($this->table)->where($where)->where('created_date', '<=', date('Y-m-d'))->first();
    }

    public function updateLog($user_id, $data = [])
    {
        //领取
        $where['user_id'] = $user_id;
        return DB::table($this->table)->where($where)->update($data);
    }

    public function updateValue($user_id, $diamond)
    {
        $where['user_id'] = $user_id;
        return DB::table($this->table)->where($where)->increment('diamond_value', $diamond);
    }

    public function getAllNum($created_at)
    {
        $sql = "SELECT rank_three,count(user_id) as count_user FROM hd_lover_diamond_consumer_log WHERE created_at < '$created_at' AND diamond_value >= 100000  GROUP BY rank_three ORDER BY rank_three ASC ";
        return  DB::connection('mysql')->select($sql);

    }


}